package com.wlxk.boot.starter.swagger;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;

import static springfox.documentation.builders.BuilderDefaults.nullToEmpty;

/**
 * @author wlxk
 * @since 2021-10-27 14:09:05
 */
@ConditionalOnProperty(value = "swagger.ui.enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SwaggerUiWebMvcConfiguration {
    private final SwaggerProperties swaggerProperties;

    @Bean
    public SwaggerUiWebMvcConfigurer swaggerUiConfigurer() {
        return new SwaggerUiWebMvcConfigurer(this.fixup(swaggerProperties.getUi().getBaseUrl()));
    }

    private String fixup(String swaggerBaseUrl) {
        return StringUtils.trimTrailingCharacter(nullToEmpty(swaggerBaseUrl), '/');
    }
}
